<?php

namespace app\admin\model\share;

use addons\shopro\model\User;
use addons\shopro\model\Order;
use addons\shopro\model\OrderItem;
use app\admin\model\share\Money as ShareMoney;
use think\Db;

class CommonShare
{
    /**
     * 注册绑定上下级
     */
    public function add_share($user_id, $parent_id)
    {
        $user = (new ShareUser())->get(['user_id' => $user_id]); // 分销用户
        if (empty($user)) {
            $user =  new ShareUser();
            $user->user_id = $user_id;
            $user->parent_id = $parent_id;
            $user->addtime = time();
        } else {
            $user->parent_id = $parent_id;
        }
        if ($user->save()) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 用户下单奖励
     */
    public function add_share_order($order_id, $goods_id, $user_id)
    {
        if (empty($goods_id)) {
//            \think\log::error('goods_id不能为空');
            return 0;
        }
        $shopro_order = (new Order())->where(['id' => $order_id])->find();
        if (empty($shopro_order)) {
            //            \think\log::error('shopro_order不能为空');
            return 0;
        }
        if ($shopro_order['status'] != 1) {
            //           \think\log::error('status 不能为1');
            return 0;
        }

        $user = (new ShareUser())->get(['user_id' => $user_id]); // 分销用户
        if (empty($user)) { // 未添加 添加分销商
//            \think\log::error('user 为空');
            return 0;
        }

        // 区域补贴
        $city_user = (new ShareUser())->get(['province_id' => $shopro_order['province_id'],'city_id'=>$shopro_order['city_id'],'role'=>3]);
        if (!empty($city_user)) {
            $this->add_money(4, $order_id, $city_user->user_id, 3, $goods_id, 3);
        }
        $area_user = (new ShareUser())->get(['province_id' => $shopro_order['province_id'],'city_id'=>$shopro_order['city_id'],'area_id'=>$shopro_order['area_id'],'role'=>2]);
        if (!empty($area_user)) {
            $this->add_money(4, $order_id, $area_user->user_id, 2, $goods_id, 3);
        }

        $parent_user = (new ShareUser())->get(['user_id' => $user->parent_id]); // 分销上级用户
        if (!empty($parent_user) &&  $parent_user->role > 0) {
            // 直推奖励
            $this->add_money(1, $order_id, $user->parent_id, $parent_user->role, $goods_id, 1);
            // 间推奖励
            $this->add_range( $order_id,  $goods_id, $parent_user->parent_id, $parent_user->role);
        }
        
        if ($user->role == 0) {
            $user->role = 1;
            $user->save();
        }
        // 用户自购升级
        $this->add_role($user_id);
        return 1;
    }

    /**
     * 极差平级奖励
     * parent_id  上级的上级id
     * role 级别
     */
    private function add_range( $order_id,  $goods_id, $parent_id, $role)
    {
        $user = (new ShareUser())->get(['user_id' => $parent_id]); // 分销用户
        if (empty($user)) {
            return 0;
        }

        if ($user->role == 3) {
            // 平级奖
            if ($role == 3) {
                $this->add_money(3, $order_id, $user->user_id, $user->role, $goods_id, 4);
            } else {//极差奖
                $c_type = ($role == 2) ? 6 : 5;
                $this->add_money(2, $order_id, $user->user_id, $user->role, $goods_id, $c_type);
            }
            return 0;
        }

        if ($user->role == 2) {
             // 平级奖
             if ($role == 2) {
                $this->add_money(3, $order_id, $user->user_id, $user->role, $goods_id, 4);
            } else {//极差奖
                $this->add_money(2, $order_id, $user->user_id, $user->role, $goods_id, 5);
            }
            return $this->add_range($order_id,  $goods_id, $user->parent_id, $role);
        }

        if ($user->role == 1) {
            return $this->add_range($order_id,  $goods_id, $user->parent_id, $role);
        }
    }

    // 级别 1 视力健康大使 2：区域服务商 3：城市服务商
    // type 1 直推 2.极差 3.平级 4 补贴
    private function add_money($type, $order_id, $user_id, $role, $goods_id, $c_type)
    {   
        // 类型 1:直推 2：复购 3：补贴 4：平级 5：间推1 6：间推2
        $con_ddata = Db::name("share_config")->where(['role' => $role, 'type' => $c_type, 'goods_id' => $goods_id])->find();
        if (!empty($con_ddata)) {
            $ShareMoney = new ShareMoney();
            $ShareMoney->money = $con_ddata['reward'];
            $ShareMoney->type = $type;
            $ShareMoney->order_id = $order_id;
            $ShareMoney->user_id = $user_id;
            $ShareMoney->goods_id = $goods_id;
            $ShareMoney->role = $role;
            $ShareMoney->addtime = time();
            $ShareMoney->is_grant = 0;
            $ShareMoney->save();

           
            $user = (new ShareUser())->get(['user_id' => $user_id]); // 分销用户 
            //$share_total_price = ShareMoney::where(['user_id'=>$user_id,'type'=>1])->sum('money');//直推
            $user_total_price = Order::where(['user_id'=>$user_id])->where('status', 'in', array(1,2))->sum('pay_fee');//自购
            $userShare = ShareUser::where('parent_id', $user_id)->select();
            $share_id_arr = array_column($userShare, 'user_id');
            if ($share_id_arr) {
                $share_total_price = Order::where('user_id', 'in', $share_id_arr)->where('status', 'in', array(1,2))->sum('pay_fee');//直推
            } else {
                $share_total_price = 0;
            }
            
            $temp_config2 = Db::name("share_config")->where(['role' => 2, 'type' => 7])->find();
            $reward2 = empty($temp_config2) ? 0 : $temp_config2['reward'];
            $temp_config3 = Db::name("share_config")->where(['role' => 3, 'type' => 7])->find();
            $reward3 = empty($temp_config3) ? 0 : $temp_config3['reward'];
            $total_price = $user_total_price + $share_total_price ;
            if ($total_price >$reward2 && $user->role < 2 && $reward2 != 0) {
                $user->role  = 2;
            }
            if ($total_price >$reward3 && $user->role < 3 && $reward3 != 0) {
                $user->role  = 3;
            }
            $user->price += $con_ddata['reward'];
            $user->total_price += $con_ddata['reward'];
            $user->save();
        }
    }

    /**
     * 用户复购升级
     */
    public function add_role($user_id)
    {
        $user = (new ShareUser())->get(['user_id' => $user_id]); // 分销用户 
        //$share_total_price = ShareMoney::where(['user_id'=>$user_id,'type'=>1])->sum('money');//直推
        $userShare = ShareUser::where('parent_id', $user_id)->select();
        $share_id_arr = array_column($userShare, 'user_id');
        if ($share_id_arr) {
            $share_total_price = Order::where('user_id', 'in', $share_id_arr)->where('status', 'in', array(1,2))->sum('pay_fee');//直推
        } else {
            $share_total_price = 0;
        }
        $user_total_price = Order::where(['user_id'=>$user_id])->where('status', 'in', array(1,2))->sum('pay_fee');//自购
        $total_price = $user_total_price + $share_total_price ;
        $temp_config2 = Db::name("share_config")->where(['role' => 2, 'type' => 7])->find();
        $reward2 = empty($temp_config2) ? 0 : $temp_config2['reward'];
        $temp_config3 = Db::name("share_config")->where(['role' => 3, 'type' => 7])->find();
        $reward3 = empty($temp_config3) ? 0 : $temp_config3['reward'];
        if ($total_price >$reward2 && $user->role < 2 && $reward2 != 0) {
            $user->role  = 2;
            $user->save();
        }
        if ($total_price >$reward3 && $user->role < 3 && $reward3 != 0) {
            $user->role  = 3;
            $user->save();
        }
    }

    /**
     * 退单
     */
    public function Chargeback($order_id,$goods_id, $user_id)
    {
        $this->fall_role($user_id);
        $ShareMoney = ShareMoney::where(['order_id'=>$order_id,'goods_id'=>$goods_id])->select();
        foreach ($ShareMoney as $key => $value) {
            $this->fall_role($value->user_id);
        }
        ShareMoney::where(['order_id'=>$order_id,'goods_id'=>$goods_id])->delete();
    }

    /**
     * 用户降级
     */
    public function fall_role($user_id)
    {
        $user = (new ShareUser())->get(['user_id' => $user_id]); // 分销用户 
        //$share_total_price = ShareMoney::where(['user_id'=>$user_id,'type'=>1])->sum('money');//直推
        $userShare = ShareUser::where('parent_id', $user_id)->select();
        $share_id_arr = array_column($userShare, 'user_id');
        if ($share_id_arr) {
            $share_total_price = Order::where('user_id', 'in', $share_id_arr)->where('status', 'in', array(1,2))->sum('pay_fee');//直推
        } else {
            $share_total_price = 0;
        }
        $user_total_price = Order::where(['user_id'=>$user_id])->where('status', 'in', array(1,2))->sum('pay_fee');//自购
        $total_price = $user_total_price + $share_total_price ;
        $temp_config2 = Db::name("share_config")->where(['role' => 2, 'type' => 7])->find();
        $reward2 = empty($temp_config2) ? 0 : $temp_config2['reward'];
        $temp_config3 = Db::name("share_config")->where(['role' => 3, 'type' => 7])->find();
        $reward3 = empty($temp_config3) ? 0 : $temp_config3['reward'];
        if ($user_total_price < 2980 && $user->role == 1) {
            $user->role  = 0;
            $user->save();
        }
        if ($total_price < $reward2 && $user->role == 2 && $reward2 != 0) {
            $user->role  = 1;
            $user->save();
        }
        if ($total_price < $reward3 && $user->role == 3 && $reward3 != 0) {
            $user->role  = 2;
            $user->save();
        }
    }

    /**
     * 佣金发放
     */
    public function grant($order_id)
    {
        $shopro_order = (new Order())->where(['id' => $order_id])->find();
        if (empty($shopro_order)) {
            return 0;
        }
        $ShareMoney = ShareMoney::where(['order_id'=>$order_id,'is_grant'=>0])->select();
        foreach ($ShareMoney as $key => $value) {
            $user = User::get($value->user_id);
            if ($user) {
                \addons\shopro\model\User::money($value->money, $value->user_id, 'commission_income', $value->order_id, '', $value);
                $temp_ShareMoney = ShareMoney::get($value->id);
                $temp_ShareMoney->is_grant = 1; 
                $temp_ShareMoney->save();
                $user = (new ShareUser())->get(['user_id' => $value->user_id]); // 分销用户 
                $user->price -= $value->money;
                $user->save();
            }
        }
    }
    
}
